<?

  // configuration parameters
  $config['NamesTableFile'] = '../data/BacNames.csv';
  $config['TreesDirectory'] = '../data/HGTrees';
  $config['TreesExtension'] = 'phb';
  $config['n'] = 10;
  $config['p'] = 15;
  $config['b'] = 20;



  // used classes
  require_once 'HOGTree.class.php';
  require_once 'TreeParser.class.php';

  // reading names table from file
  say( "Reading names table ... " );
  $bacNamesFile = file($config['NamesTableFile']);
  if ( $bacNamesFile === null )
    die( "Can't read names table file '$config[NamesTableFile]'." );
  if ( trim($bacNamesFile[0]) !== "abbbrev_hogenom,species_hogenom" )
    die( "Unexpected header line in names table file:\n$bacNamesFile[0]." );
  unset($bacNamesFile[0]);
  $bacNames = array();
  foreach ( $bacNamesFile as $row )
    if ( !empty($row) )
    {
      list($abbr,$name) = explode( ',', trim($row) );
      $abbr = preg_replace( "~_.*$~", "", $abbr );
      $bacNames[$abbr] = $name;
    }
  unset($bacNamesFile);
  asort($bacNames);
  HOGTree::$bacIDs = array_keys($bacNames);
  say("done\n\n");
  // finished reading names table from file

  // reading trees
  say( "Started reading and pre-filtering trees.\n\n" );
  $treeFilesMask = "$config[TreesDirectory]/*.$config[TreesExtension]";
  $treeFilesList = glob($treeFilesMask);
  if ( empty($treeFilesList) )
    die( "No files found matching mask '$treeFilesMask'." );
  $treeFiles = array();
  foreach ( $treeFilesList as $fileName )
  {
    $treeName = preg_replace( "~\.$config[TreesExtension]$~", "",
                              basename( $fileName ) );
    $treeFiles[$treeName] = $fileName;
  }

  $tn = 0;
  $tc = count($treeFiles);
  $hogTrees = array();
  foreach ( $treeFiles as $treeName => $fileName )
  {
    ++$tn;
    say( "Input file: '$treeName' ($tn/$tc):\n" );
    say( "Reading file ... ");
    $newick_tree = file_get_contents($fileName);
    if ( empty($newick_tree) )
      die( "No input." );
    say("done\n");

    say("Parsing tree ... ");
    $tParser = new TreeParser($newick_tree);
    $trees = $tParser->getTrees();
    if ( $tParser->errorOccured() )
      die( "Tree parser error: \"" . $tParser->getErrorMessage() . "\".\n" );
    else
      say("done\n");
    unset($tParser); // don't need a parser any more

    if ( empty($trees) )
      die( "No tree found." );
    $tree = $trees[0];

    say("Parsing labels ... ");
    $hogTree = new HOGTree($tree); // making a tree with parsed labels
    $tree->destroy(); // freeng the memory...
    unset($tree);
    say("done\n");

    $skip = false;

    $nL = $hogTree->numberOfLeaves();
    $nmin = $config['n'];
    if ( $nL < $nmin )
    {
      say("Number of leaves = $nL < $nmin.\n");
      $skip = true;
    }
    else
      say("Number of leaves = $nL.\n");

    $nBac = $hogTree->numberOfBacLeaves();
    $nmin = $config['n'];
    if ( $nBac < $nmin )
    {
      say("Number of bacterial leaves = $nBac < $nmin.\n");
      $skip = true;
    }
    else
      say("Number of bacterial leaves = $nBac.\n");

//    if ( !$skip and !$hogTree->hasNonBacLeaf() )
    if ( !$skip and $nBac === $nL )
    {
      say("All leaves are bacterial.\n");
      $skip = true;
    }

    if ( !$skip )
    {
      $hogTrees[$treeName] = $hogTree;
      say("Added the tree to S1.\n");
    }
    else
      say("Skipped the tree.\n");

    say( "\n" );

  }
  say( "Finished reading and pre-filtering trees.\n" );
  say( "Number of trees in S1 - " . count($hogTrees) . ".\n\n" );
  // finished reading trees

say( "----- debuging data -----\n" );
say( "'B' means 'Bacterium', 'A' means... well... 'not A bacterium.'\n" );
say( "Tge d values in labels are multiplied by 100 and rounded to integer.\n" );
say( "\n" );
  foreach ( $hogTrees as $hogName => $hogTree )
  {
    $hogTree->calcD_rec();
say( "Tree '$hogName':\n" . $hogTree->toNewickCompact() . "\n" );
    $dList = $hogTree->getDList();
    arsort($dList);
say( "The 10 greatest d values:\n" );
$counter = 0;
foreach ( $dList as $id => $d )
{
  say( "$counter: " . number_format( $d, 4 ) . "\n" );
  if ( $counter == 0 )
    $dm = $d;
  if ( $counter == 1 )
    $dpm = $d;
  if ( ++$counter == 10 )
    break;
}
echo "\n";
$p = sqrt($dm) - sqrt($dpm);
say( "p = $p\n" );
echo "\n";

  }


  function say( $text )
  {
    echo $text;
  }

?>
